<?php

namespace App\Store\Models\OldMaterial;

use App\Common\Services\Common;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;
use Illuminate\Database\Eloquent\Model;

class ReturnFactory extends Model
{
    protected $table = 'site_oldmaterial_return';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     *获取列表
     */
    public static function getList($request)
    {
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::with(['startstoreinfo', 'targetstoreinfo', 'infolist' => function ($query) {
            $query->where('is_del', 1);
        }]);

        $store_ids = StoreCommon::getCurrentAccountStoreId($request);
        if ($store_ids) { //权限控制
            if (is_array($store_ids)) { //获取当前权限下的门店
                $query->whereIn('start_store_id', $store_ids);
            }
            if (is_numeric($store_ids) && intval($store_ids) > 0) { //获取当前门店下的列表
                $query->where('start_store_id', intval($store_ids));
            }
        }

        $start_store_id = $request['start_store_id'] ?? 0;
        if ($start_store_id > 0) {
            $query->where('start_store_id', $start_store_id);
        }
        $target_store_id = $request['target_store_id'] ?? 0;
        if ($target_store_id > 0) {
            $query->where('target_store_id', $target_store_id);
        }
        $ids = $request->ids;
        $ids = is_array($ids) ? $ids : (is_array(json_decode($ids, true)) ? json_decode($ids, true) : []);
        if (count($ids) > 0) {
            $query->whereIn('id', $ids);
        }
        //供应商
        $supply_id = $request['supply_id'] ?? 0;
        if ($supply_id > 0) {
            $query->where('supply_id', $supply_id);
        }

        //订单状态
        $audit_status = $request['audit_status'] ?? 0;
        if ($audit_status > 0) {
            $query->where('audit_status', $audit_status);
        }

        //编号
        $order_sncode = $request['order_sncode'] ?? '';
        if ($order_sncode != '') {
            $query->where('order_sncode', 'like', '%' . $order_sncode . '%');
        }

        $start_time = $request['start_time'] ?? '';
        $end_time = $request['end_time'] ?? '';
        if (!empty($start_time) && !empty($end_time)) {
            $start_time = (!is_numeric($start_time) && strtotime($start_time)) > 0 ? strtotime($start_time) : intval($start_time);
            $end_time = (!is_numeric($end_time) && strtotime($end_time) > 0) ? strtotime($end_time) : intval($end_time);
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }

        $query->where('is_del', 1)->where('cp_id', $cp_id);
        $query->orderBy('create_at', 'desc');

        $isgetall = $request['isgetall'] ?? 0;
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    /**
     * 外联明细表
     */
    public function infolist()
    {
        return $this->hasMany(AllotInfolist::class, 'p_id', 'id');
    }

    /**
     * 店铺
     */
    public function startstoreinfo()
    {
        return $this->hasOne(SiteStore::class, 'id', 'start_store_id')->select('id', 'store_name');
    }

    public function targetstoreinfo()
    {
        return $this->hasOne(SiteStore::class, 'id', 'target_store_id')->select('id', 'store_name');
    }
}

